package com.mac.intellect_flow.admin.controller.system;

import com.mac.intellect_flow.reply.PageParam;
import com.mac.intellect_flow.reply.PageResult;
import com.mac.intellect_flow.reply.R;
import com.mac.intellect_flow.sys.entity.SysPost;
import com.mac.intellect_flow.sys.service.SysPostService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 岗位信息操作处理
 * 
 * @author mac
 */
@Tag(name = "岗位管理")
@RestController
@RequestMapping("/system/post")
public class SysPostController {
    @Autowired
    private SysPostService postService;

    /**
     * 获取岗位列表
     */
    @Operation(summary = "岗位分页列表")
    @PreAuthorize("@ps.hasPermi('system:post:list')")
    @GetMapping("/list")
    public R<PageResult<SysPost>> list(SysPost post, PageParam pageParam) {
        return R.success(postService.queryPage(post, pageParam));
    }
    
    /*@PreAuthorize("@ps.hasPermi('system:post:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysPost post)
    {
        List<SysPost> list = postService.selectPostList(post);
        ExcelUtil<SysPost> util = new ExcelUtil<SysPost>(SysPost.class);
        util.exportExcel(response, list, "岗位数据");
    }*/

    /**
     * 根据岗位编号获取详细信息
     */
    @Operation(summary = "根据岗位编号获取详细信息")
    @PreAuthorize("@ps.hasPermi('system:post:query')")
    @GetMapping(value = "/{postId}")
    public R getInfo(@PathVariable Long postId) {
        return R.success(postService.selectPostById(postId));
    }

    /**
     * 新增岗位
     */
    @Operation(summary = "新增岗位")
    @PreAuthorize("@ps.hasPermi('system:post:add')")
    @PostMapping
    public R add(@Validated @RequestBody SysPost post) {
        if (!postService.checkPostNameUnique(post)) {
            return R.error("新增岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        } else if (!postService.checkPostCodeUnique(post)) {
            return R.error("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        return R.success(postService.insertPost(post));
    }

    /**
     * 修改岗位
     */
    @Operation(summary = "修改岗位")
    @PreAuthorize("@ps.hasPermi('system:post:edit')")
    @PutMapping
    public R edit(@Validated @RequestBody SysPost post) {
        if (!postService.checkPostNameUnique(post)) {
            return R.error("修改岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        } else if (!postService.checkPostCodeUnique(post)) {
            return R.error("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        return R.success(postService.updatePost(post));
    }

    /**
     * 删除岗位
     */
    @Operation(summary = "删除岗位")
    @PreAuthorize("@ps.hasPermi('system:post:remove')")
    @DeleteMapping("/{postIds}")
    public R remove(@PathVariable Long[] postIds) {
        return R.success(postService.deletePostByIds(postIds));
    }

    /**
     * 获取岗位选择框列表
     */
    @Operation(summary = "获取岗位选择框列表")
    @GetMapping("/optionselect")
    public R optionselect() {
        List<SysPost> posts = postService.selectPostAll();
        return R.success(posts);
    }
}
